from __future__ import with_statement
from lsocket import LSocket, LSocketMaintenance
import sys
from threading import Thread

class SenderThread(Thread):
    def __init__(self, data):
        super(SenderThread, self).__init__()
        self._data = data
    
    def run(self):
        sock = LSocket()
        sock.connect(('', 23456))
        length = len(self._data)
        sock.send(str(length))
        print 'server said', sock.recv(1024)
        bytes_sent = 0
        while bytes_sent < length:
            bytes_sent += sock.send(self._data[bytes_sent:])
        print 'server said', sock.recv(1024)
        sock.close()

LSocket.set_upload_limit(256)
try:    
    with open(sys.argv[1], 'r') as infile:
        data = infile.read()
    t1 = SenderThread(data)
    t2 = SenderThread(data)
    t1.start()
    t2.start()
    t1.join()
    t2.join()

finally:
    LSocketMaintenance.shutdown()
